package com.java110.mall.shop.bmo.storeInfo.impl;

import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileDto;
import com.java110.dto.file.FileRelDto;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.shop.IStoreInfoInnerServiceSMO;
import com.java110.po.file.FileRelPo;
import com.java110.po.store.StoreInfoPo;
import com.java110.mall.shop.bmo.storeInfo.ISaveStoreInfoBMO;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service("saveStoreInfoBMOImpl")
public class SaveStoreInfoBMOImpl implements ISaveStoreInfoBMO {

    @Autowired
    private IStoreInfoInnerServiceSMO storeInfoInnerServiceSMOImpl;

    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     * 添加小区信息
     *
     * @param storeInfoPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(StoreInfoPo storeInfoPo) {
        String photo = storeInfoPo.getIcon();
        storeInfoPo.setIcon("");
        storeInfoPo.setStoreInfoId(GenerateCodeFactory.getGeneratorId("11"));
        doSavaFile(photo, storeInfoPo.getStoreInfoId(), FileRelDto.REL_TYPE_CD_STORE_PIC);
        int flag = storeInfoInnerServiceSMOImpl.saveStoreInfo(storeInfoPo);
        if (flag > 0) {
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }


    /**
     * 保存图片
     *
     * @param photo
     * @param objId
     */
    private void doSavaFile(String photo, String objId, String fileTypeCd) {
        if (StringUtil.isEmpty(photo)) {
            return;
        }

        FileDto fileDto = new FileDto();
        fileDto.setCommunityId("-1");
        fileDto.setContext(photo);
        fileDto.setFileId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
        fileDto.setFileName(fileDto.getFileId());

        fileDto.setSuffix("jpeg");
        String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
        FileRelPo fileRelPo = null;
        fileRelPo = new FileRelPo();
        fileRelPo.setObjId(objId);
        fileRelPo.setRelTypeCd(fileTypeCd);
        fileRelPo.setFileRelId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_relId));
        fileRelPo.setFileRealName(fileName);
        fileRelPo.setFileSaveName(fileName);
        fileRelPo.setSaveWay(FileRelDto.SAVE_WAY_FTP);
        int save = fileRelInnerServiceSMOImpl.saveFileRel(fileRelPo);

        if (save < 1) {
            throw new IllegalArgumentException("保存文件失败");
        }
    }
}
